/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * This mixin handles the creation of an action bar
 */
qx.Mixin.define("bip.ui.form.MActionBar",
{
  
  
  members: {
  	__actionBarButtons: null,
	
  	/**
	 * Create the action bar
	 * 
	 * properties: {
	 * 	buttons: [
	 * 		{ type: <build-in type>,
	 * 		  id: "button-id",
	 * 		  label: "button label",
	 * 		  icon: "button-icon",
	 * 		  command: "button-command" },
	 * 		{ ... <button 2 properties>...}, ...
	 * 			 ]
	 * }
	 * 
	 * @param properties The bar properties
	 * @param container The container that will handles the created action bar
	 * 
	 */
	populateActionBar: function(properties, container) {
		if (container == null) {
			container = this.getChildControl("actionbar");
		}
		if (properties == null) { return null;}
		if (properties.buttons == null) { return null;}
		if (this.__actionBarButtons == null) {this.__actionBarButtons = {};}
		// Add buttons
		for (var i=0; i<properties.buttons.length;i++) {
			var button = this.createButton(properties.buttons[i]);
			//this.debug("Adding button '"+bProps.id+"' to bar");
			container.add(button);
			this.registerButton(button);
		}
	},
	
	createButton:function(bProps) {
		var button;
		if (bProps.type != null) {
			var buildinTypeFuncName = "new"+bProps.type;
			if (bip.ui.form.PredefinedButtons[buildinTypeFuncName]) {
				button = bip.ui.form.PredefinedButtons[buildinTypeFuncName](bProps.label);
			} else {
				throw new Error("Button predifined type '"+bProps.type+"' does not exists !");
			}
		}
		if (button == null) {
			button = new qx.ui.form.Button(bProps.label,bProps.icon);
		}
		if (bProps.command != null) {
			button.setCommand(bProps.command);
		}
		if (bProps.tooltipText != null) {
			button.setToolTip(new qx.ui.tooltip.ToolTip(bProps.tooltipText));
		}
		if (bProps.id == null) { bProps.id = button.getLabel();}
		button.setUserData("id",bProps.id);
		return button;
	},
	
	registerButton:function(button) {
		var id = button.getUserData("id");
		if (id == null) {
			this.error("Button must have an id through a userdata property named 'id'");
			return;
		}
		if (this.__actionBarButtons[id] != null) {
			this.error("A button with id '"+id+"' is already registered ! Skipping registration !");
			return;
		}
		this.__actionBarButtons[id] = button;
	},
	
	createActionBar: function(properties) {
		var control = new qx.ui.container.Composite(new qx.ui.layout.HBox(10));
		control.add(new qx.ui.core.Spacer(),{flex:1});
		this.populateActionBar(properties,control);
		return control;
	},
	
	/**
	 * Return the button referenced by its id
	 * @param buttonId {String} The id of the button
	 * @return the button or null
	 */
	getActionBarButton: function(buttonId) {
		if (this.__actionBarButtons == null) {return null;}
		return this.__actionBarButtons[buttonId];
	},
	
	/**
	 * Applies properties to all registered buttons
	 */
	setActionBarButtonsProperties: function(properties) {
		for (var buttonId in this.__actionBarButtons) {
			this.__actionBarButtons[buttonId].set(properties);
		}
	}
		
  }
  
});